package com.example.server.service.admin.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.server.common.constant.ArticleConstants;
import com.example.server.common.constant.EnableConstants;
import com.example.server.common.constant.Result;
import com.example.server.entity.Article;
import com.example.server.mapper.ArticleMapper;
import com.example.server.service.admin.ArticleService;
import com.example.server.vo.ArticleVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author china.fuyao@outlook.com
 * @date 2020-04-08 11:11
 */
@Service("articleService")
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    /**
     * 修改文章
     *
     * @param articleVO
     * @param customerId
     * @return
     */
    @Override
    public Result updateArticleById(ArticleVO articleVO, Integer customerId) {
        Article article = this.getById(articleVO.getArticleId());
        if (null == article) {
            return Result.Error("文章不存在");
        }
        article.setArticleTitle(articleVO.getArticleTitle());
        article.setArticleSubhead(articleVO.getArticleSubhead());
        article.setArticleContent(articleVO.getArticleContent());
        LocalDateTime now = LocalDateTime.now();
        article.setUpdateTime(now);
        article.setUpdateUid(customerId);
        boolean update = this.updateById(article);
        if (update) {
            return Result.OK("操作成功");
        }
        return Result.Error("操作失败");
    }

    /**
     * 新增帮助文档
     *
     * @param articleVO
     * @param customerId
     * @return
     */
    @Override
    public Result addArticleByHelp(ArticleVO articleVO, Integer customerId) {
        Article article = new Article();
        article.setArticleType(ArticleConstants.HELP_ARTICLE);
        article.setArticleTitle(articleVO.getArticleTitle());
        article.setArticleSubhead(articleVO.getArticleSubhead());
        article.setArticleContent(articleVO.getArticleContent());
        article.setReadNum(0);
        article.setArticleStatus(EnableConstants.ENABLE);
        LocalDateTime date = LocalDateTime.now();
        article.setAddTime(date);
        article.setAddUid(customerId);
        article.setUpdateTime(date);
        article.setUpdateUid(customerId);
        boolean save = this.save(article);
        if (save) {
            return Result.OK("操作成功");
        }
        return Result.Error("操作失败");
    }

    /**
     * 获取指定文章详情
     *
     * @param articleId
     * @return
     */
    @Override
    public Result infoById(Integer articleId) {
        Map<String, Object> map = baseMapper.articleInfoById(articleId);
        if (null == map || MapUtil.getInt(map, "article_type") == ArticleConstants.HELP_ARTICLE
                && MapUtil.getInt(map, "article_status") == EnableConstants.DISABLE) {
            return Result.Error("文章不存在");
        }
        //更新阅读数
        Article article = new Article();
        article.setArticleId(articleId);
        article.setReadNum(MapUtil.getInt(map, "read_num") + 1);
        updateById(article);
        return new Result(map);
    }

    /**
     * 系统文档列表
     *
     * @param current
     * @param size
     * @param key
     * @return
     */
    @Override
    public Result sysArticleList(Integer current, Integer size, String key) {
        IPage page = new Page(null == current ? 1 : current, null == size ? 10 : size);
        IPage<Map> customerIPage = baseMapper.articleList(page, ArticleConstants.SYSTEM_ARTICLE, key, null);
        return new Result(customerIPage);
    }

    /**
     * 帮助文档列表
     *
     * @param current
     * @param size
     * @param key
     * @return
     */
    @Override
    public Result helpArticleList(Integer current, Integer size, String key, Integer enable) {
        IPage page = new Page(null == current ? 1 : current, null == size ? 10 : size);
        IPage<Map> customerIPage = baseMapper.articleList(page, ArticleConstants.HELP_ARTICLE, key, enable);
        return new Result(customerIPage);
    }

    /**
     * 修改帮助文档状态
     *
     * @param articleId
     * @param enable
     * @return
     */
    @Override
    public Result updateArticleStatusByHelp(Integer articleId, Integer enable, Integer customerId) {
        Article article = this.getById(articleId);
        if (null == article || article.getArticleType() != ArticleConstants.HELP_ARTICLE) {
            return Result.Error("文章不存在");
        }
        article.setArticleStatus(enable);
        article.setUpdateTime(LocalDateTime.now());
        article.setUpdateUid(customerId);
        boolean update = updateById(article);
        if (update) {
            return Result.OK("操作成功");
        }
        return Result.Error("操作失败");
    }
}
